package com.nanhai.buddhist.college.module.collegebase.controller.admin.textbookinfo.info;

import com.nanhai.buddhist.college.module.collegebase.controller.admin.textbookinfo.info.vo.*;
import com.nanhai.buddhist.college.module.collegebase.convert.textbookinfo.info.TextbookInfoConvert;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;

import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.textbookinfo.TextbookInfoDO;
import com.nanhai.buddhist.college.module.collegebase.service.textbookinfo.info.TextbookInfoService;

@Api(tags = "管理后台 - 教材列表")
@RestController
@RequestMapping("/collegebase/textbook-info")
@Validated
public class TextbookInfoController {

    @Resource
    private TextbookInfoService textbookInfoService;

    @PostMapping("/create")
    @ApiOperation("创建教材列表")
    @PreAuthorize("@ss.hasPermission('collegebase:textbook-info:create')")
    public CommonResult<String> createTextbookInfo(@Valid @RequestBody TextbookInfoCreateReqVO createReqVO) {
        return success(textbookInfoService.createTextbookInfo(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新教材列表")
    @PreAuthorize("@ss.hasPermission('collegebase:textbook-info:update')")
    public CommonResult<Boolean> updateTextbookInfo(@Valid @RequestBody TextbookInfoUpdateReqVO updateReqVO) {
        textbookInfoService.updateTextbookInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除教材列表")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:textbook-info:delete')")
    public CommonResult<Boolean> deleteTextbookInfo(@RequestParam("id") String id) {
        textbookInfoService.deleteTextbookInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得教材列表")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:textbook-info:query')")
    public CommonResult<TextbookInfoRespVO> getTextbookInfo(@RequestParam("id") String id) {
        TextbookInfoDO textbookInfo = textbookInfoService.getTextbookInfo(id);
        return success(TextbookInfoConvert.INSTANCE.convert(textbookInfo));
    }

    @GetMapping("/list")
    @ApiOperation("获得教材列表列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('collegebase:textbook-info:query')")
    public CommonResult<List<TextbookInfoRespVO>> getTextbookInfoList(@RequestParam("ids") Collection<String> ids) {
        List<TextbookInfoDO> list = textbookInfoService.getTextbookInfoList(ids);
        return success(TextbookInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得教材列表分页")
    @PreAuthorize("@ss.hasPermission('collegebase:textbook-info:query')")
    public CommonResult<PageResult<TextbookInfoRespVO>> getTextbookInfoPage(@Valid TextbookInfoPageReqVO pageVO) {
        PageResult<TextbookInfoDO> pageResult = textbookInfoService.getTextbookInfoPage(pageVO);
        return success(TextbookInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出教材列表 Excel")
    @PreAuthorize("@ss.hasPermission('collegebase:textbook-info:export')")
    @OperateLog(type = EXPORT)
    public void exportTextbookInfoExcel(@Valid TextbookInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<TextbookInfoDO> list = textbookInfoService.getTextbookInfoList(exportReqVO);
        // 导出 Excel
        List<TextbookInfoExcelVO> datas = TextbookInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "教材列表.xls", "数据", TextbookInfoExcelVO.class, datas);
    }


    @GetMapping("/allList")
    @ApiOperation("获得所有教材列表")
    public CommonResult<List<TextbookInfoRespVO>> getTextbookInfoAllList() {
        List<TextbookInfoDO> list = textbookInfoService.getTextbookInfoAllList();
        return success(TextbookInfoConvert.INSTANCE.convertList(list));
    }



}
